package com.example.demo4.service;

import com.example.demo4.domain.Teacher;

import com.example.demo4.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {

    @Autowired

    // Service 需要引用 Repository 中的方法
    // Repository 中的方法有些是自己生成的

    private TeacherRepository teacherRepository;

    public  List<Teacher> findByTnameLike(String name) {
        List<Teacher> teachers = teacherRepository.findByTnameLike(name);
        return teachers;
    }

    /*
    * 查询所有
     */
    public List<Teacher> findAll(){
        // findAll()自带
        return teacherRepository.findAll();
    }
    /*
     * 按工号查询
     */
    public Teacher findByTno(String tno) {
        //findByTno 自定义
        return teacherRepository.findByTno(tno);
    }
    /*
     * 查询工号最大的老师
     */
    public Teacher findTnoMax(Sort sort){
        // findAll()自带
        List<Teacher> list = teacherRepository.findAll(sort);
        return list.get(0);
    }

    public void insert(Teacher teacher){
        // save自带
        teacherRepository.save(teacher);
    }

    public void update(Teacher Teacher){
        teacherRepository.save(Teacher);
    }

//    public void update2(Teacher Teacher){
//        teacherRepository.update(Teacher);
//    }
//
    public void delete(String tno){
        teacherRepository.deleteByTno("2020413310");
    }

}
